package com.lgl.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lgl.constants.SystemConstants;
import com.lgl.entity.Comment;
import com.lgl.entity.ResponseResult;
import com.lgl.entity.User;
import com.lgl.enums.AppHttpCodeEnum;
import com.lgl.exception.SystemException;
import com.lgl.mapper.CommentMapper;
import com.lgl.service.CommentService;
import com.lgl.service.UserService;
import com.lgl.utils.BeanCopyUtils;
import com.lgl.vo.CommentVo;
import com.lgl.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private UserService userService;

    @Override
    public ResponseResult getCommentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
//        根据文章id查询评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(articleId),Comment::getArticleId, articleId);
        queryWrapper.eq(Comment::getRootId, -1);
        queryWrapper.eq(Comment::getType,commentType);
//        分页查询
        Page<Comment> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page, queryWrapper);
        List<CommentVo> commentVoList = getCommentVoList(page.getRecords());
        for (CommentVo commentVo : commentVoList) {
            commentVo.setChildren(getChildren(commentVo.getId()));
        }
//        封装返回
        return ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        if (!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return ResponseResult.okResult();
    }

    private List<CommentVo> getChildren(Long id){
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> list = list(queryWrapper);
        List<CommentVo> commentVoList = getCommentVoList(list);
        return commentVoList;

    }
    private List<CommentVo> getCommentVoList(List<Comment> list) {
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        for (CommentVo commentVo : commentVos) {
            User user = userService.getById(commentVo.getCreateBy());
            commentVo.setUsername(user.getNickName());
            if (commentVo.getToCommentUserId()!=-1){
            User user1 = userService.getById(commentVo.getToCommentUserId());
                commentVo.setToCommentUserName(user1.getNickName());
            }
        }
        return commentVos;

    }
}

